package org.distributed.transaction.core.process;

import org.distributed.transaction.core.context.TransactionContext;
import org.distributed.transaction.core.exception.TransactionException;

/**
 * 事务处理器
 * 
 * @author chenfanglin
 */
public interface TransactionProcessor {

	/**
	 * 业务代码执行前
	 * 
	 * @param transactionContext
	 * @throws TransactionException
	 */
	public void preBusinessCode(TransactionContext transactionContext) throws TransactionException;

	/**
	 * 执行业务代码
	 *
	 * @param transactionContext
	 * @return Object Object
	 * @throws Throwable Throwable
	 */
	default Object doBusinessCode(TransactionContext transactionContext) throws Throwable {
		return transactionContext.getBusinessCallback().callback();
	}

	/**
	 * 业务代码执行失败
	 *
	 * @param transactionContext
	 * @param throwable          throwable
	 * @throws TransactionException TransactionException
	 */
	void onBusinessCodeError(TransactionContext transactionContext, Throwable throwable) throws TransactionException;

	/**
	 * 业务代码执行成功
	 *
	 * @param transactionContext
	 * @param result             result
	 * @throws TransactionException TransactionException
	 */
	void onBusinessCodeSuccess(TransactionContext transactionContext, Object result) throws TransactionException;

	/**
	 * 清场
	 *
	 * @param transactionContext
	 */
	void clear(TransactionContext transactionContext)  throws TransactionException ;
}
